package cn.com.cfae.iras.doc.analyze.parse.tokenizer;

import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import com.hankcs.hanlp.corpus.document.sentence.Sentence;
import com.hankcs.hanlp.corpus.document.sentence.word.CompoundWord;
import com.hankcs.hanlp.corpus.document.sentence.word.IWord;
import com.hankcs.hanlp.corpus.document.sentence.word.Word;
import com.hankcs.hanlp.tokenizer.NLPTokenizer;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * M1-释义章节分词
 */
public class SYTokenizer extends BaseTokenizer {


    private static Logger logger = LoggerFactory.getLogger(SYTokenizer.class);

    private SYTokenizer() {
    }

    private static SYTokenizer instance = new SYTokenizer();

    public static SYTokenizer getInstance() {
        return instance;
    }

    public ExtractItem splitWords(ExtractBizModel extractBizModel, String text) {
        if (extractBizModel == null) {
            return null;
        }
        if (StringUtils.isEmpty(text)) {
            return null;
        }
        Sentence sentence = NLPTokenizer.ANALYZER.analyze(text);
        List<IWord> wordList = sentence.wordList;
        IWord iWord = null;
        ExtractItem extractItem = null;
        for (int i = 0; i < wordList.size(); i++) {
//            iWord = wordList.get(i);
//            String value = iWord.getValue().trim();
//            if (extractBizModel.getSynonyms().contains(value)) {
//                extractItem = extractBizModel.getExtractItemBySynonym(value);
//                logger.info("分析到释义业务指标内容，指标名称：{}，分析文本：{}。", extractItem.getItemName(), value);
//                break;
//            }
            iWord = wordList.get(i);
            if (iWord instanceof CompoundWord) {
                CompoundWord compoundWord = (CompoundWord) iWord;
                List<Word> innerWordList = compoundWord.innerList;
                for (int j = 0; j < innerWordList.size(); j++) {
                    IWord innerWord = innerWordList.get(j);
                    String value = innerWord.getValue().trim();
                    if (extractBizModel.getSynonyms().contains(value)) {
                        if (j >= 5) {
                            continue;
                        }
                        extractItem = extractBizModel.getExtractItemBySynonym(value);
                        logger.info("分析发行机构业务指标模型，分析到业务模型及业务指标，IteName：{}，指标名称：{}。", extractItem.getItemName(), value);
                        break;
                    }
                }
                if (extractItem != null) {
                    break;
                }
            } else {
                String value = iWord.getValue().trim();
                if (extractBizModel.getSynonyms().contains(value)) {
                    if (i >= 5) {
                        continue;
                    }
                    extractItem = extractBizModel.getExtractItemBySynonym(value);
                    logger.info("分析发行机构业务指标模型，分析到业务模型及业务指标，IteName：{}，指标名称：{}。", extractItem.getItemName(), value);
                    break;
                }
            }
        }
        return extractItem;
    }


    public ExtractItem splitWords2(ExtractBizModel extractBizModel, String text) {
        if (extractBizModel == null) {
            return null;
        }
        if (StringUtils.isEmpty(text)) {
            return null;
        }
        Sentence sentence = NLPTokenizer.ANALYZER.analyze(text);
        List<IWord> wordList = sentence.wordList;
        IWord iWord = null;
        ExtractItem extractItem = null;
        for (int i = 0; i < wordList.size(); i++) {
            iWord = wordList.get(i);
            String value = iWord.getValue().trim();
            if (extractBizModel.getSynonyms().contains(value)) {
                extractItem = extractBizModel.getExtractItemBySynonym(value);
                KVText kText = new KVText();
                kText.setText(joint(wordList, i));
                extractItem.setKey(kText);
                logger.info("分析到释义业务指标内容，extractItem：{}，文本：{}。", extractItem.getItemName(), value);
                break;
            }
        }
        return extractItem;
    }

    private String joint(List<IWord> wordList, int offset) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < offset; i++) {
            stringBuilder.append(wordList.get(i));
        }
        return stringBuilder.toString();
    }
}
